<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AlbumController
 *
 * @author koifl_000
 */
class CollectionController extends FrontEndController {

    public function filters() {
        return array(
            'accessControl',
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     *
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', 'actions' => array('create', 'edit', 'saveimage', 'removeimage', 'delete', 'like', 'comment'),
                'roles' => array('member')
            ),
            array('allow', 'actions' => array('view', 'user'),
                'users' => array('*')
            ),
            array('deny', 'users' => array('*')
            )
        );
    }

    public function actionUser($username) {
        $user = User::model()->find('username=:username', array(':username' => $username));
        if ($user != null) {
            $model = Collection::model()->findAll('user_created=:user_id', array(':user_id' => $user->id));
            $this->render('user', array('model' => $model, 'user' => $user));
        } else {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
    }

    public function actionCreate() {
        $model = new Collection();
        if (isset($_POST['Collection'])) {
            $model->attributes = $_POST['Collection'];
            $model->created_at = date("Y-m-d H:i:s");
            $model->user_created = Yii::app()->user->id;
            if ($model->save()) {
                $this->redirect(array('view', 'id' => $model->id));
            }
        }
        $this->render('create', array('model' => $model));
    }

    public function actionEdit($id) {
        $model = $this->loadCollection($id);
        if ($model->userCreated->id != Yii::app()->user->id) {
            throw new CHttpException(404, 'The requested page does not exist.');
        } else {
            if (isset($_POST['Collection'])) {
                $model->attributes = $_POST['Collection'];
                if ($model->save()) {
                    $this->redirect(array('view', 'id' => $model->id));
                }
            }
            $this->render('edit', array('model' => $model));
        }
    }

    public function actionView($id) {
        $model = $this->loadCollection($id);
        $allComment = CommentCollection::model()->findAll('collection_id=:collection_id and status = :status', array(':collection_id' => $id, ':status' => CommentCollection::COMMENT_ACTIVE));
        $images = CollectionImage::model()->findAll('collection_id=:id', array(':id' => $id));
        $comment = new CommentCollection ();
        $this->render('view', array('model' => $model, 'images' => $images, 'comment' => $comment, 'allComment' => $allComment));
    }

    public function actionSaveImage($id) {
        $model = new CollectionImage();
        $sql = 'SELECT ci.* FROM tbl_collection_image ci join tbl_collection c on ci.collection_id = c.id where ci.image_id =:image_id and c.user_created =:user';
        $imageInCollection = CollectionImage::model()->findAllBySql($sql, array(':image_id' => $id, ':user' => Yii::app()->user->id));
        $collections = CHtml::listData(Collection::model()->findAll('user_created=:user_id', array(':user_id' => Yii::app()->user->id)), 'id', 'name');
        $image = Image::model()->findByPk($id);
        if ($image != null) {
            if (isset($_POST['CollectionImage'])) {
                $model->attributes = $_POST['CollectionImage'];
                $model->image_id = $id;
                if ($model->save()) {
                    $this->redirect(array('view', 'id' => $model->collection_id));
                }
            }
        } else {
            throw new CHttpException(404, 'The requested page does not exist.');
        }

        $this->render('saveimage', array('model' => $model, 'image' => $image, 'collections' => $collections, 'imageInCollection' => $imageInCollection));
    }

    public function actionRemoveImage($image_id, $collection_id) {
        $image = Image::model()->findByPk($image_id);
        $collection = Collection::model()->findByPk($collection_id);
        if ($image != null && $collection != null) {
            $model = CollectionImage::model()->find('image_id=:image_id and collection_id=:collection_id', array(':image_id' => $image_id, ':collection_id' => $collection_id));
            if ($model != null && $collection->user_created == Yii::app()->user->id) {
                $model->delete();
                $this->redirect(array('view', 'id' => $collection_id));
            }
        } else {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
    }

    public function actionDelete($id) {
        $model = Collection::model()->findByPk($id);
        if ($model->user_created == Yii::app()->user->id) {
            if ($model != null) {
                $model->delete();
                $images = CollectionImage::model()->findAll('collection_id=:id', array(':id' => $id));
                foreach ($images as $image) {
                    $image->delete();
                }
                $this->redirect(array('user', 'username' => $model->userCreated->username));
            }
        } else {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
    }

    public function actionLike($id) {
        $result = array();
        $collection = $this->loadCollection($id);
        $check = CollectionLike::model()->find("user_id = :user_id and collection_id =:collection_id", array(
            ':collection_id' => $id,
            'user_id' => Yii::app()->user->id
        ));
        if ($check) {
            $check->delete();
            $collection->like_cnt = $image->like_cnt - 1;
            $collection->update();
            $result ['meg'] = 'Thích';
        } else {
            $model = new CollectionLike ();
            $model->collection_id = $id;
            $model->user_id = Yii::app()->user->id;
            if ($model->insert()) {
                $userActive = new UserActive ();
                $userActive->user_id = Yii::app()->user->id;
                $userActive->collection_id = $id;
                $userActive->active_at = date('Y-m-d H:i:s');
                $userActive->active_type = UserActive::LIKE_COLLECTION;
                $userActive->insert();
            }
            $collection->like_cnt = $collection->like_cnt + 1;
            $collection->update();
            $result ['meg'] = 'Bỏ thích';
        }

        $result ['like_cnt'] = $collection->like_cnt;

        echo json_encode($result);
    }

    public function actionComment($id) {
        $comment = new CommentCollection();
        if (isset($_POST ['CommentCollection'])) {
            $comment->attributes = $_POST ['CommentCollection'];
            $comment->comment_at = date("Y-m-d H:i:s");
            $comment->user_id = Yii::app()->user->id;
            $comment->collection_id = $_GET ['id'];
            $comment->content = CHtml::encode($comment->content);
            if ($comment->save()) {
                $userActive = new UserActive ();
                $userActive->active_type = UserActive::COMMENT_COLLECTION;
                $userActive->active_at = date('Y-m-d H:i:s');
                $userActive->collection_id = $_GET ['id'];
                $userActive->user_id = Yii::app()->user->id;
                $userActive->save();

                $this->redirect(array(
                    'view',
                    'id' => $id
                ));
            }
        }
        $this->redirect(array(
            'view',
            'id' => $id
        ));
    }

    public function loadCollection($id) {
        $model = Collection::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}

?>
